#include "updatevaluesdialog.h"

UpdateValuesDialog::UpdateValuesDialog( QMap<QString, QStringList> m )
{
setupUi(this);

QIcon green = QIcon( QPixmap(":/binary/green_b.png") );
QIcon yellow = QIcon( QPixmap(":/binary/yellow_b.png") );
QIcon red = QIcon(QPixmap(":/binary/red_b.png") );
QIcon blue = QIcon(QPixmap(":/binary/blue_b.png") );
comboBox->setMaxVisibleItems(15);

attrs = m;
status_column = -1;

Schema * schema = new Schema;
    if ( NULL == schema )
    {
    return;
    }
QMap<int, QString> headers = schema->headers();
QList<int> strings = schema->strings();
attrs_map = schema->attrs_map(); 
status_column = schema->status_column();

    for ( int i = 1; i < headers.size(); ++i )
    {
        if ( true == strings.contains(i) )
        {
        comboBox->addItem( green, headers.value(i) );
        }
        else
        {
        comboBox->addItem( yellow, headers.value(i) );
        }
    }
    
comboBox->setCurrentIndex(status_column - 1);
valueComboBox->addItems( attrs.value("statuses") );

delete schema;

QObject::connect( comboBox,
                  SIGNAL( currentIndexChanged(int) ),
                  this,
                  SLOT( update_box(int) ) );    
}

UpdateValuesDialog::~UpdateValuesDialog()
{
}

void UpdateValuesDialog::update_box( int i )
{
i += 1;

valueComboBox->clear();

QString attr_str = QLatin1String("");
attr_str = attrs_map.value(i);

    if ( i ==  status_column)
    {
    valueComboBox->setEditable(false);
    }
    else
    {
    valueComboBox->setEditable(true);
    }

    if ( true != attr_str.isEmpty() )
    {
    QStringList l = attrs.value(attr_str);
        if ( true != l.isEmpty() )
        {
        valueComboBox->addItems(l);
        }
    }
    
    if ( i != status_column )
    {
    valueComboBox->setCurrentIndex(-1);
    }

}

QMap<QString, QVariant> UpdateValuesDialog::return_selection()
{
QMap<QString, QVariant> mr;
mr["index"] = QVariant( comboBox->currentIndex() );
mr["name"] = QVariant( comboBox->currentText() );
    if ( true == nullCheckBox->isChecked() )
    {
    mr["is_null"] = QVariant( QString( QLatin1String("Y") ) );
    mr["value"] = QVariant( QString( QLatin1String("") ) );
    }
    else
    {
    mr["is_null"] = QVariant( QString( QLatin1String("N") ) );
    mr["value"] = QVariant( valueComboBox->currentText() );
    }
return mr;
}
